﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HDTuyenChon.AppCode;

namespace HDTuyenChon.BL
{
	public class TaiKhoanBL
	{						
		/// <summary>
		/// Lay quyen cua user dang login
		/// </summary>
		/// <returns>-1: lay quyen khong thanh cong, nguoc lai la quyen đang đăng nhập</returns>
		public int getQuyenLogin(TaiKhoan tk)
		{
			int quyen = -1;
			try
			{
				using (AppCode.HDFilmDataContext db = new HDFilmDataContext())
				{
					var users = (from u in db.TaiKhoans
								 where u.UserName == tk.UserName && u.Password == tk.Password
								 select u).SingleOrDefault();
					if (users != null)
					{
						quyen = 1;
					}
				}
			}
			catch (Exception exp)
			{
				throw new Exception("TaiKhoanBL.cs - login - " + exp.Message.ToString(), exp);
			}
			
			return quyen;
		}
		
		/// <summary>
		/// Kiem quyen dang nhap
		/// </summary>
		/// <returns>True: login thanh cong, False: login that bai</returns>
		public TaiKhoan login(string taiKhoan,string matKhau)
		{
			TaiKhoan tk = null;
			try{
			using(AppCode.HDFilmDataContext db = new HDFilmDataContext())
			{
				var user = (from u in db.TaiKhoans
						 where u.UserName == taiKhoan && u.Password == matKhau
						 select u).SingleOrDefault();
				if (user != null)
				{
					tk = (TaiKhoan)user;
				}
			}
			}catch (Exception exp)
			  {
				  throw new Exception("TaiKhoanBL.cs - login - " + exp.Message.ToString(), exp);
			  }

			return tk;
		}

	}
}